<template>
<div class="login-page">
  <el-form :model="user" :rules="rule" label-width="80" ref="registerForm">
    <div class="form-label">
      <h1>注册表</h1>
    </div>
    <el-form-item label="账号" prop="username">
      <el-input type="text" v-model="user.username"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input type="text" v-model="user.email"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="user.nickname"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="user.password" show-password></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPassword">
      <el-input type="password" v-model="user.checkPassword" show-password></el-input>
    </el-form-item>
    <div class="register-button">
      <el-button :loading="loading" type="primary" style="width: 100%" @click="toRegister">注册</el-button>
    </div>
  </el-form>
</div>
</template>

<script>
import { ref } from 'vue'
import { userRegister } from '@/api/user'
import { ElMessage } from 'element-plus'
import 'element-plus/es/components/message/style/css'

export default {
  name: 'UserRegister',
  setup () {
    const user = ref({
      nickname: '',
      username: '',
      password: '',
      email: '',
      checkPassword: ''
    })
    const checkPassword = function (rule, value, callback) {
      if (value) {
        if (user.value.password !== user.value.checkPassword) {
          callback(new Error('密码不一致'))
        }
      }
      callback()
    }
    return {
      user,
      loading: ref(false),
      rule: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        nickname: [],
        checkPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { required: true, validator: checkPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    toRegister () {
      this.loading = true
      this.$refs.registerForm.validate((val) => {
        if (val) {
          userRegister(this.user).then((val) => {
            this.loading = false
            ElMessage({
              showClose: true,
              grouping: true,
              message: '账号激活链接已发送到您的邮箱，请留意邮箱信息',
              type: 'success'
            })
            this.$refs.registerForm.resetFields()
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.register-button{
  margin-left: 50px;
  margin-right: 20px;
}
.login-page{
  width: 600px;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 600px;
  background-color: #e3eeef;
}
.form-label{
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 30px 30px;
  padding-top: 0;
}
.login-addition{
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.el-form{
  width: 300px;
}
</style>
